Skip to content

fix(cli): re-attach the driver after a restart leaves a stale session - #77

Merged
tenequm merged 2 commits into
mainfrom
fix/pw-reattach-stale-session
Sep 18, 2026
Merged

tenequm merged 2 commits into
mainfrom
fix/pw-reattach-stale-session

Conversation

@tenequm

@tenequm tenequm commented Sep 18, 2026

Copy link
Copy Markdown
Member

What

cuttle pw and cuttle jev-browse auto-attach the bundled driver when a verb fails for want of a session, but they only recognized one of the driver's two "not open" wordings. After docker kill + up, an unclean restart, or the in-container driver daemon dying (pkill -9 -f cliDaemon.js), the driver's session file survives while its socket is dead, and every verb then failed with a Node stack trace until a manual cuttle pw attach.

Why

The pinned driver (@playwright/cli 0.1.20, playwright-core 1.64.0-alpha-2026-09-14, read from the built image) has two messages:

  • lib/tools/cli-client/output.js:74 - The browser '<session>' is not open, please run open first - no session file at all (a clean stop).
  • lib/tools/cli-client/session.js:60 - Browser '<session>' is not open. Run - thrown uncaught when a session file exists but its socket cannot be connected.

The wrapper matched only is not open, please run, and its comment claimed that was the only possible wording.

Change

  • playwrightNeedsAttach (shared by the cuttle pw path and the jev-browse runner) now matches either is not open, please run or is not open. Run. Still tied to the pinned driver version, as before; the comment now names both sources and when each fires.
  • Tests: the stale-session wording in TestPlaywrightNeedsAttach (and attach failing on it stays real), plus TestPlaywrightRunnerReattaches, which drives newPlaywrightRunner against a fake sessionless driver for both wordings and asserts verb, attach, retry. Both new cases fail on main.
  • The knowledge finding on the attach model documents both wordings.

Verification

Live, on a throwaway --ephemeral container of an image built from main (the fix is CLI-side, so the image is unchanged), with each branch's CLI:

Scenario main this PR
pkill -9 -f cliDaemon.js, then pw snapshot exit 1, session.js:60 stack trace exit 0, snapshot
docker kill + docker start, then pw snapshot exit 1, same trace exit 0, snapshot
docker restart, then pw snapshot exit 0 (clean stop removes the session file) exit 0
stale session, then jev-browse --mock --url https://example.com exit 1 on goto, same trace loop runs (exit 4, mock step budget)

just check passes.

Release notes

cuttle pw and cuttle jev-browse now re-attach on their own after a container is killed or restarted uncleanly, or the in-container driver dies, instead of failing every verb with a Node stack trace until a manual cuttle pw attach.

@tenequm
tenequm marked this pull request as ready for review September 18, 2026 21:34
@tenequm
tenequm merged commit bbeb0f0 into main Sep 18, 2026
9 checks passed
@tenequm
tenequm deleted the fix/pw-reattach-stale-session branch September 18, 2026 21:34
tenequm added a commit that referenced this pull request Sep 18, 2026
## What was broken

A lifecycle stress run (after #77/#78, which fixed its blockers) left
these:

1. **`pw --takeover --name X <verb>` hit the wrong instance.**
`--name`/`--context` were peeled off before `--takeover`, so instance
flags after `--takeover` went to the driver while the lease was
force-released on the *default* instance.
2. **Old images advertised a driver they lack.** Against a container
from an image that predates the bundled playwright-cli (0.14.2 and
earlier), the briefing advertised `pw` and `jev-browse`, and both failed
with a raw `OCI runtime exec failed ... executable file not found` (exit
127).
3. **`up --recreate` switched images silently.** Without `--image` it
moves the container to the CLI's default image and the briefing hid it.
4. **A driving verb right after `docker restart` failed** with a raw
`reaching the session lease: exit status 7` (daemon not listening yet)
or `exit status 127` (exec into the driver workdir while the daemon
recreates it).
5. **`open --until` ignored a dead browser**, blocking the whole
`--timeout` and then printing a stale URL.
6. Smaller: status said `up --recreate` "discards the profile" (it keeps
it); a repeated `down --purge` claimed "removed container"; `--cdp-port
0` and `--cdp-port X --vnc-port X` were accepted (and the latter left an
orphan volume); an invalid `--name` reached docker and was printed
unquoted in hints; a failed `docker run` always ran `docker rm -f
<name>`, even when it failed on a name conflict with a concurrent `up`
that won.
7. Deferred from #78: the ssh standing tunnel counted as healthy if its
pid lived and the local port accepted connections - which is also what
another process holding that port looks like. And the direct backend's
not-running hint said to run `up`, which direct refuses.

## What changed

- **pw flags:** `splitCuttleFlags` peels `--context`, `--name` and
`--takeover` in any order from the leading run; anything from the verb
on is still the driver's.
- **Driver detection:** a cheap in-container probe (`sh -c 'command -v
playwright-cli'`, run in `/`) decides whether the briefing shows the
driver block or a note naming `<cuttle> up --recreate`. `pw` recognizes
the exec's 127 "not found" and returns the same hint; `jev-browse`
probes before taking the lease. An exec that fails outright is not read
as a missing driver. The daemon has no version endpoint and older ones
never will, so the container itself is the only reliable source.
- **Image on recreate:** kept the upgrade semantics (`up --recreate`
without `--image` moves to this CLI's image - that is the upgrade path
the new hint relies on) and print `note: image changed <old> -> <new>`
when it changes. `SSH` gains `Image` so this works over ssh too.
- **Lease after restart:** `leaseCall` retries curl's exit 7 for up to
20s and execs in `/` instead of the driver's workdir. Only the lease
call changed in `lease.go`.
- **`open --until`:** a websocket failure (new `errCDPConn`) ends the
wait at once with the last URL. It deliberately does not re-dial:
`/json` relaunches a crashed browser, and its blank tab would satisfy a
`gone:` predicate and read as a finished sign-in.
- **up/down/status:** `checkPorts` before any docker call; docker's
container-name rule enforced in `resolve` for local/ssh; `checkPorts`
runs after port discovery, so `up --recreate --cdp-port <the viewer's
port>` is refused before teardown; failed runs skip cleanup on a name
conflict and remove the profile volume only when docker said it did not
exist beforehand (an inspect that errors counts as present); wording
fixes.
- **Tunnel identity:** the daemon's root now reports `hostname` (the
container's). The ssh tunnel health check compares it with the remote
container's `Config.Hostname`; a mismatch respawns the forward, and a
mismatch after respawn stops the supervisor and fails with the port. A
daemon that reports no hostname (older image, or not up yet) passes as
before. k8s is unchanged.
- **Direct hints:** `status`, `open` and `pw` on a direct context that
does not answer say to start that browser yourself; its briefing says
`cuttle pw` needs a container.

## Verification

Throwaway containers only (`lf-*`, ports 9710-9712/6710-6712), config
isolated via `XDG_*`:

- `pw --takeover --name lf-a press Tab` with a lease held on lf-a: lease
freed on lf-a, verb ran; the default instance's lease endpoint
untouched.
- `docker restart lf-a` then `pw goto`: main fails 3/3 (`reaching the
session lease: exit status 127`), this branch succeeds 3/3 in about 2s.
- `open --until title:never --timeout` with `docker kill` 6s in: main
waits the full timeout; this branch returns in 6s with `lost the page
while waiting; last at https://example.com/`. Success paths (`title:`, a
`url:` reached by navigation) still resolve.
- 0.14.2 container: main briefing advertises the driver and
`pw`/`jev-browse` fail with the OCI error; this branch shows the upgrade
note and `pw snapshot`, `pw click`, `jev-browse --mock` all fail with
the hint. `up --recreate --image cuttle:lf-fix` prints `image changed
ghcr.io/glim-sh/cuttle:0.14.2 -> cuttle:lf-fix`, the driver block comes
back, and the root endpoint's `hostname` matches `docker inspect
.Config.Hostname`.
- Bad ports, `--cdp-port 0` and `--name 'lf bad'` are refused before any
container or volume exists; a repeated `down --purge` says the instance
was already gone; four concurrent `up`s of one instance leave the winner
running (losers hit the name conflict and no longer remove it).
- A direct context that does not answer: main says run `up`, this branch
says start it yourself.

The ssh tunnel identity check is unit-tested
(`TestTunnelReachesInstance`); there was no ssh host to test it on.
`just check` is green.

## Release notes

`cuttle pw --takeover` now works in any order with `--name`/`--context`.
Against an image older than the bundled driver, the briefing and
`pw`/`jev-browse` say to run `up --recreate`. `up --recreate` names an
image change. Verbs right after a restart wait for the daemon.
tenequm added a commit that referenced this pull request Sep 21, 2026
🤖 I have created a release *beep* *boop*
---


## [0.15.0](v0.14.2...v0.15.0)
(2026-09-21)


### Features

* bundle playwright-cli into the image with a cuttle pw passthrough
([#67](#67))
([28d9846](28d9846))
* **cli:** center the skill and briefing on cuttle pw, drop host driver
routing ([#75](#75))
([33eca16](33eca16))
* **cli:** colored jev-browse output
([#70](#70))
([cf6f109](cf6f109))
* **cli:** cuttle jev-browse - autonomous browsing loop on the bundled
driver ([#66](#66))
([0496957](0496957))
* **cli:** deliver pw action snapshots as a host file, secrets masked
([#103](#103))
([78adbbb](78adbbb))
* **cli:** make --context/--name global so pw and jev-browse reach any
instance ([#74](#74))
([9df5c33](9df5c33))
* **cli:** zero-ceremony cuttle pw - auto-attach on demand, allow open
([a7e6491](a7e6491))
* **jev:** withhold write actions and halve jev-browse run time
([#102](#102))
([9ca9386](9ca9386))
* **serve:** mask password field values in /snapshot
([#109](#109))
([2a352b5](2a352b5))
* **serve:** session lease - one driver at a time, with takeover
([#69](#69))
([87724c8](87724c8))


### Bug Fixes

* **backend:** keep profile cleanup and remote argv from reaching past
their own ([#92](#92))
([f52cfd3](f52cfd3))
* **cli:** close the gaps a review of the lifecycle fixes found
([#90](#90))
([c46aa5e](c46aa5e))
* **cli:** close the lifecycle gaps a stress run left open
([#88](#88))
([899a535](899a535))
* **cli:** discarding the profile removes the instance's host snapshot
dir ([#113](#113))
([57cd07a](57cd07a))
* **cli:** downloads --wait accepts a finished download, logs drops
D-Bus spam ([#111](#111))
([dcc2f46](dcc2f46))
* **cli:** name the in-page dialog behind a pw click timeout
([#99](#99))
([11beeeb](11beeeb))
* **cli:** never fall back to another instance when the selected one is
stopped ([#78](#78))
([a1df361](a1df361))
* **cli:** re-attach the driver after a restart leaves a stale session
([#77](#77))
([bbeb0f0](bbeb0f0))
* **cli:** run --extract on every ending, not only done
([#71](#71))
([4954195](4954195))
* **jev:** judge done on the page that landed, and end done on a none
above even odds ([#108](#108))
([a9cb5a8](a9cb5a8))
* **jev:** offer filter-apply controls the write gate withheld
([#114](#114))
([bdf440a](bdf440a))
* **jev:** read quoted snapshot lines and keep field values out of
--extract ([#89](#89))
([09111a2](09111a2))
* **pw:** compact find output, downloads into a directory, quieter logs
([#116](#116))
([62f2ca8](62f2ca8))
* **pw:** snapshot links a host file like an action, and the console log
line names its verb
([#110](#110))
([c902c58](c902c58))
* **serve:** dismiss a native dialog the driver leaves open
([#87](#87))
([14f24cb](14f24cb))
* **serve:** keep the default seed's downloads across browser relaunch
([aac8e40](aac8e40))
* **serve:** keep the dialog watch alive through a signal shutdown
([#94](#94))
([4f4f1d0](4f4f1d0))
* **serve:** pool-mode reaping, capture tab, hung tabs and launch bursts
([#86](#86))
([3b42ea2](3b42ea2))
* **serve:** skip an idle reap whose timer a launch has since re-armed
([#93](#93))
([c667868](c667868))
* **smoke:** never exec into a container the run was not pointed at
([#79](#79))
([9075244](9075244))
* **viewer:** keep websocket inside proxy prefix
([#62](#62))
([3b10ba7](3b10ba7))


### Performance Improvements

* **cli:** run a pw verb in one docker exec, lease check included
([#100](#100))
([a9c3c90](a9c3c90))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant